/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Database;

import java.sql.*;
import java.util.Properties;

/**
 *
 * @author Sinem
 */
public class Manager {
    private static Manager databaseManager;
    private static boolean m_bDBConnected = false;
    private static Connection connection;
    private static String sWebSite;
    private Manager() {
        //Load JDBC Driver if the web site is available
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void setWebSite(String sSite)
    {
        sWebSite = sSite;
    }
    public static Manager getInstance() {
        if (databaseManager == null)
        {
          databaseManager = new Manager();
        }
        return databaseManager;
    }

    public boolean connectToDB() {
      boolean bSuccessful = true;
      
      String url = "jdbc:postgresql://localhost/WSSCV";
      Properties props = new Properties();
      props.setProperty("user", "postgres");
      props.setProperty("password", "ihsan");
      try
      {
      connection = DriverManager.getConnection(url, props);
      m_bDBConnected = true;
      }catch(Exception e){
          e.printStackTrace();
          m_bDBConnected = false;
      }      
      return bSuccessful;
  }
  synchronized public boolean storeToDB (ResultTableItem item)
  {
      boolean bSuccessful = false;
      if (m_bDBConnected == false)
      {
          connectToDB();
      }
     try{
      PreparedStatement st = connection.prepareStatement
              ("INSERT INTO result(WebSite,WebPage,bRuleViolated,LineNumber,ColumnNumber,AppliedRuleId) " +
              "VALUES(?,?,?,?,?,?)");
      st.setString(1, this.sWebSite);
      st.setString(2, item.sWebPage);
      st.setBoolean(3,item.bRuleViolated);
      st.setInt(4,item.nLineNumber);
      st.setInt(5,item.nColumnNumber);
      st.setString(6, item.sRuleId);
      st.execute();
      st.close();
     }catch(Exception e){
         e.printStackTrace();
     }
      return bSuccessful;
  }
}
